package com.netty.service;

import com.alibaba.fastjson.JSON;
import com.netty.constant.Constants;
import com.netty.model.*;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.Date;
import java.util.List;

/**
 * 图灵机器人
 *
 * @author suzhen
 * @create 2018/7/16
 */
@Setter
@Slf4j
public class TuLingRobotServiceImpl implements RobotService {
    /**
     * 图灵机器人 apiUrl
     */
    private String apiUrl;
    /**
     * 秘钥  请自行申请图灵机器人KEY
     */
    private String key;

    @Override
    public MessageWrapper botMessageReply(String user, String content) {
        log.info("TLRebot reply user -->" + user + "--mes:" + content);
        StringBuilder message = new StringBuilder();
        try {
            Document doc = Jsoup.connect(apiUrl).timeout(62000).data("key", key).data("userid", user).data("info", content).post();
            String msgStr = doc.select("body").html().replace("&quot;", "'");
            msgStr = msgStr.replace("\r\n", "<br>");
            msgStr = msgStr.replace("<br />", "");
            RobotMessage msg = JSON.parseObject(msgStr, RobotMessage.class);
            switch (msg.getCode()) {
                case 100000:
                    message = new StringBuilder(msg.getText());
                    break;
                case 200000:
                    message = new StringBuilder(msg.getText() + "<a href='" + msg.getUrl() + "' target='_blank;'>" + msg.getUrl() + "</a></br>");
                    break;
                case 302000:
                    List<RobotMessageArticle> sublist = JSON.parseArray(msg.getList(), RobotMessageArticle.class);
                    for (RobotMessageArticle a : sublist) {
                        //message+="a("+a.getDetailurl()+")["+a.getArticle()+"] <br><br>"; //layim聊天窗口回复内容
                        message.append("<a href='").append(a.getDetailurl()).append("' target='_blank;'>").append(a.getArticle()).append("</a></br>");
                    }
                    break;
                case 308000:
                    List<RobotMessageArticle> sublistOne = JSON.parseArray(msg.getList(), RobotMessageArticle.class);
                    for (RobotMessageArticle a : sublistOne) {
                        //message+="菜名："+a.getName()+"<br>用料："+a.getInfo()+"  <br>详情地址:a("+a.getDetailurl()+")["+a.getDetailurl()+"]      <br><br>";//layim聊天窗口回复内容
                        message.append("菜名：").append(a.getName()).append("<br>用料：").append(a.getInfo()).append("  <br>详情地址:<a href='").append(a.getDetailurl()).append("' target='_blank;'>").append(a.getDetailurl()).append("</a>      </br>");
                    }
                    break;
                default:
                    break;
            }

            Message result = new Message();
            result.setCmd(Constants.CmdType.MESSAGE);
            result.setType(Constants.ProtobufType.REPLY);
            //机器人ID
            result.setSender(Constants.ImserverConfig.REBOT_SESSIONID);
            //回复人
            result.setReceiver(user);
            result.setTimeStamp(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            MessageBody messageBody = new MessageBody();
            messageBody.setContent(message.toString());
            result.setContent(JSON.toJSONString(messageBody));
            return new MessageWrapper(MessageWrapper.MessageProtocol.REPLY, Constants.ImserverConfig.REBOT_SESSIONID, user, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MessageWrapper(MessageWrapper.MessageProtocol.REPLY, Constants.ImserverConfig.REBOT_SESSIONID, user, null);
    }
}
